package ca.ets.osgi.planeservice;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;
import java.util.ArrayList;


public class FlightReader {
	
	private ListAirport la;
	
	/**
	 * CONSTRUCTOR
	 */
	public FlightReader(){}
	
	public void openList(){
		la = ListAirport.getInstance();
		try {
			System.err.println("[Trying to load and fill the list of Airports]");
			//File fXmlFile = new File("C://eclipse/Workspace/ca.ets.osgi.planeservice/src/ca/ets/osgi/planeservice/flights.xml");
			File fXmlFile = new File("C://temp/ca.ets.osgi.planeservice/src/ca/ets/osgi/planeservice/flights.xml");
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			Document doc = dBuilder.parse(fXmlFile);
			doc.getDocumentElement().normalize();
			
			NodeList airportList = doc.getElementsByTagName("Airport");
			ArrayList<Airport> list = new ArrayList<Airport>();
			
			//creating a list of airport
			int pix = airportList.getLength();
			for (int temp = 0; temp < airportList.getLength(); temp++) {
				Node airportNode = airportList.item(temp);
				
				if (airportNode.getNodeType() == Node.ELEMENT_NODE) {
					Element elem = (Element) airportNode;
					Airport aport = new Airport();
					
					aport.setAirportName(getValue("nom",elem).trim());
					aport.setAirportID(elem.getAttribute("aid").trim());
					aport.setPhone(getValue("phone",elem).trim());
					
					Adresse adr = new Adresse();
					adr.setAdresseNum(Integer.parseInt(getValue("noadresse",elem).trim()));
					adr.setRue(getValue("rue",elem).trim());
					adr.setVille(getValue("ville",elem).trim());
					adr.setProvince(getValue("province",elem).trim());
					adr.setCodePostal(getValue("codepostal",elem).trim());
					
					aport.setAdresse(adr);
					
					NodeList flightList = elem.getElementsByTagName("flight");
					int pix2 = flightList.getLength();
					for(int temp2 = 0; temp2 < flightList.getLength(); temp2++){
						Node flightNode = flightList.item(temp2);
						
						if(flightNode.getNodeType() == Node.ELEMENT_NODE){
							Element elem2 = (Element) flightNode;
							
							FlightInfo flight = new FlightInfo();
							flight.setNumFlight(Integer.parseInt(getValue("numFlight",elem2).trim()));
							flight.setAirportArrival(getValue("airportArrival",elem2).trim());
							flight.setAirportDeparture(getValue("airportDeparture",elem2).trim());
							flight.setCityArrival(getValue("cityArrival",elem2).trim());
							flight.setCityDeparture(getValue("cityDeparture",elem2).trim());
							flight.setDateDeparture(getValue("dateDeparture",elem2).trim());
							flight.setDateReturn(getValue("dateReturn",elem2).trim());
							flight.setFlightID(elem2.getAttribute("fid"));
							flight.setAvailability();
							
							
							FlightAbs fabs = flight;
							aport.addFlightToTable(flight.getFlightID(), fabs);
							//list.
							
						}
					}
					la.addAirportToList(aport.getAirportID(), aport);
				}
			}
			System.out.println("The List of Airports have been loaded");
			   
		}catch (Exception e) {
			e.printStackTrace();
	    }
		
		
		
	}
	
	private static String getValue(String tag, Element element) {
		NodeList nodes = element.getElementsByTagName(tag).item(0).getChildNodes();
		Node node = (Node) nodes.item(0);
		return node.getNodeValue();
	}

}
